package utils;

import java.util.Set;
import java.util.Vector;

import javax.swing.JOptionPane;

import schemaspy.Config;
import schemaspy.SchemaAnalyzer;
import core.ApplicationCore;

public final class Operations {

	private static final String OUTPUTPATH = "output";

	private Operations() {
	}

	public static void systemEnd() {
		Thread analyzer = new Thread();
		analyzer.setName("SSpy");
		analyzer.start();
		// If some alive show Warning message
		if (isThreadAlive(analyzer)) {
			int returnVal = JOptionPane.showConfirmDialog(ApplicationCore.getConnectionWizard(), "Schema Analyzer is Running!\nClosing MMDSS do NOT finalize the analyze process.\nDo you really want close MMDSS?", "Exit MMDSS", JOptionPane.YES_NO_OPTION);
			if (returnVal == JOptionPane.YES_OPTION) {
				System.exit(0);
			}
		}
		else {
			System.exit(0);
		}
	}

	public static boolean isThreadAlive(Thread t) {
		Boolean tAlive = false;
		if (t != null) {
			Set<Thread> set = Thread.getAllStackTraces().keySet();
			for (Thread at : set) {
				if (at.getName().equals("SSpy")||
						at.getName().equals("terrOut") ||
						at.getName().equals("tinOut")) {
					if (at.isAlive()) {
						tAlive = true;
					}
				}
			}
		}
		return tAlive;
	}

	public static void startSchemaAnalysis(String[] params) {

		ApplicationCore.getConnectionWizard().setVisible(false);
		ApplicationCore.getSchemaViewer().setVisible(true);

		// Build command
		String[] command = buildCommand(params);

		System.out.println(String.valueOf(command));

		// Start SchemaSpy analyzer
		SchemaAnalyzer analyzer = new SchemaAnalyzer();
		try {
			analyzer.analyze(new Config(command));
		} catch (Exception e) {
			e.printStackTrace();
		}

		//	op = new RunSchemaSpy(command);
		//	t1 = new Thread(op);
		//	t1.setName("SSpy");
		//	t1.start();
	}

	private static String[] buildCommand (String[] params) {
		Vector<String> s = new Vector<String>();

		// Params: 0-Type, 1-Host, 2-Name, 3-User, 4-Password

		// DB Type, Driver and Properties
		if (!(params[0].equals(""))) {
			String type = "";
			String driver = "";
			String properties = "";
			if(params[0].equals("MySQL")) {
				type = "mysql";
				driver = "resources/mysql-driver.jar";
				properties = "resources/mysql.properties";
			}
			else if(params[0].equals("SQL Server")) {
				type = "mssql";
				driver = "resources/mssql-driver";
				properties = "resources/mssql.properties";
			}
			else if(params[0].equals("PostgreSQL")) {
				type = "pgsql";
				driver = "resources/pgsql-driver";
				properties = "resources/pgsql.properties";

			}
			//			s += " -t " + type + " -dp " + driver + " -connprops " + properties;
			s.add("-t");
			s.add(type);
			s.add("-dp");
			s.add(driver);
			s.add("-connprops");
			s.add(properties);
		}

		// Host
		if (!params[1].equals("")) {
			//			s = s + " -host " + params[1];
			s.add("-host");
			s.add(params[1]);
		}

		// DB Name
		if (!params[2].equals("")) {
			//			s = s + " -db " + params[2];
			s.add("-db");
			s.add(params[2]);
		}

		// User
		if (!params[3].equals("")) {
			//			s = s + " -u " + params[3];
			s.add("-u");
			s.add(params[3]);
		}

		// Password
		if (!params[4].equals("")) {
			//			s = s + " -p " + params[4];
			s.add("-p");
			s.add(params[4]);
		}

		// Output
		//		s = s + " -o " + OUTPUTPATH;
		s.add("-o");
		s.add(OUTPUTPATH);

		//			// No Rows
		//			if (noRowsBox.isSelected()) {
		//				s = s + " -norows ";
		//			}

		// No HTML
		//		s.add("-nohtml");

		//			// High Quality
		//			if (highQuCBox.isSelected()) {
		//				s = s + " -hq ";
		//			}
		//
		//			// No Ads
		//			if (noAdsCBox.isSelected()) {
		//				s = s + " -noads ";
		//			}
		//
		//			// No Logo
		//			if (noLogoCBox.isSelected()) {
		//				s = s + " -nologo ";
		//			}

		String[] command = new String[s.size()];
		return s.toArray(command);
	}
}

